<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

  <xsl:strip-space elements="*" />
  <!-- Name of mainFile has to set by the caller of this style sheet -->
  <xsl:param name="mainFile" />
  <!-- Name of extraFile has to set by the caller of this style sheet -->
  <xsl:param name="extraFile" />
  <xsl:variable name="extraEnterpriseNode" select="document($extraFile)/enterprise" />

  <!-- This is the initial template. Transformation starts here! -->
  <xsl:template name="main">

    <xsl:element name="enterprise">
      <!-- Handle all subnodes (properties, person, group, membership) of the enterprice node in the mainFile -->
      <xsl:apply-templates select="document($mainFile)/enterprise" />
    </xsl:element>

  </xsl:template>

  <xsl:template match="properties">
    <properties>
      <xsl:copy-of select="type" />
    </properties>
  </xsl:template>

  <xsl:template match="person">
    <xsl:element name="person">
      <xsl:call-template name="operationAttribute" />
      <xsl:apply-templates select="sourcedid" />
      <xsl:if test="extension/pifu_id[@type='personNIN']">
      <personNIN>
        <xsl:value-of select="extension/pifu_id[@type='personNIN']/value" />
      </personNIN>
      </xsl:if>
      <xsl:if test="extension/pifu_id[@type='feideName']">
      <feideName>
        <xsl:value-of select="extension/pifu_id[@type='feideName']/pifu_value" />
      </feideName>
      </xsl:if>
      <xsl:copy-of select="userid" />
      <xsl:apply-templates select="name" />
      <xsl:apply-templates select="institutionrole" />
      <xsl:if test="email">
        <email>
          <xsl:value-of select="email" />
        </email>
      </xsl:if>
      <xsl:apply-templates select="adr" />
      <xsl:apply-templates select="tel">
        <xsl:sort select="@teltype" />
      </xsl:apply-templates>
    </xsl:element>
  </xsl:template>

  <xsl:template match="group">
    <xsl:variable name="thisId" select="sourcedid/id" />
    <xsl:choose>
      <xsl:when test="grouptype/typevalue/@level='1'">
        <xsl:call-template name="organization">
          <xsl:with-param name="extraGroup" select="$extraEnterpriseNode/group[sourcedid/id=$thisId]" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="grouptype/typevalue/@level='2'">
        <xsl:call-template name="organizationalUnit">
          <xsl:with-param name="extraGroup" select="$extraEnterpriseNode/group[sourcedid/id=$thisId]" />
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="membership">
    <xsl:element name="membership">
      <xsl:call-template name="operationAttribute" />
      <xsl:apply-templates select="sourcedid" />
      <xsl:apply-templates select="member" />
    </xsl:element>
  </xsl:template>

  <xsl:template name="operationAttribute">
    <xsl:choose>
      <xsl:when test="@recstatus='1'">
        <xsl:attribute name="operation">add</xsl:attribute>
      </xsl:when>
      <xsl:when test="@recstatus='2'">
        <xsl:attribute name="operation">modify</xsl:attribute>
      </xsl:when>
      <xsl:when test="@recstatus='3'">
        <xsl:attribute name="operation">delete</xsl:attribute>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="sourcedid">
    <xsl:copy-of select="." />
  </xsl:template>

  <xsl:template match="name">
    <xsl:element name="name">
      <xsl:element name="display">
        <xsl:value-of select="fn" />
      </xsl:element>
      <xsl:element name="family">
        <xsl:value-of select="n/family" />
      </xsl:element>
      <xsl:element name="given">
        <xsl:value-of select="n/given" />
      </xsl:element>
      <xsl:element name="prefix">
        <xsl:value-of select="n/prefix" />
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <xsl:template match="adr">
    <xsl:element name="postalAddress">
      <xsl:element name="street">
        <xsl:value-of select="street" />
      </xsl:element>
      <xsl:element name="locality">
        <xsl:value-of select="locality" />
      </xsl:element>
      <xsl:element name="pcode">
        <xsl:value-of select="pcode" />
      </xsl:element>
    </xsl:element>
    <xsl:element name="location">
      <xsl:value-of select="locality" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="tel">
    <xsl:choose>
      <xsl:when test="@teltype='1'">
        <xsl:element name="institutionPhoneNbr">
          <xsl:value-of select="." />
        </xsl:element>
      </xsl:when>
      <xsl:when test="@teltype='3'">
        <xsl:element name="mobilePhoneNbr">
          <xsl:value-of select="." />
        </xsl:element>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="institutionrole">
    <xsl:element name="institutionrole">
      <xsl:value-of select="lower-case(@institutionroletype)" />
    </xsl:element>
  </xsl:template>

  <xsl:template name="organization">
    <xsl:param name="extraGroup" />
    <xsl:element name="organization">
      <xsl:call-template name="operationAttribute" />
      <xsl:apply-templates select="sourcedid" />
      <legalName>
        <xsl:value-of select="$extraGroup/extension/pifu_name[@type='legalName']/pifu_value" />
      </legalName>
      <orgName>
        <xsl:value-of select="$extraGroup/extension/pifu_name[@type='orgName']/pifu_value" />
      </orgName>
      <orgNumber>
        <xsl:value-of select="$extraGroup/extension/pifu_id[@type='organizationNumber']/pifu_value" />
      </orgNumber>
      <fullName>
        <xsl:value-of select="$extraGroup/extension/pifu_name[@type='preferredName']/pifu_value" />
      </fullName>
      <description>
        <xsl:value-of select="$extraGroup/description/short" />
      </description>
      <orgAcronym>
        <xsl:value-of select="$extraGroup/extension/pifu_name[@type='acronym']/pifu_value" />
      </orgAcronym>
      <orgSchemaVersion>1.4.1</orgSchemaVersion>
      <email>
        <xsl:value-of select="$extraGroup/email" />
      </email>
      <xsl:apply-templates select="$extraGroup/extension/pifu_adr[@type='orgPostalAddress']/adr" />
      <phoneNbr>
        <xsl:value-of select="$extraGroup/extension/pifu_tel[@type='orgTelephone']" />
      </phoneNbr>
      <facsimileNbr>
        <xsl:value-of select="$extraGroup/extension/pifu_tel[@type='orgFax']" />
      </facsimileNbr>
      <homePageURI>
        <xsl:value-of select="$extraGroup/extension/pifu_mail[@type='homePage']" />
      </homePageURI>
      <whitePagesURI>
        <xsl:value-of select="$extraGroup/extension/pifu_mail[@type='whitePages']" />
      </whitePagesURI>
    </xsl:element>
  </xsl:template>

  <xsl:template name="organizationalUnit">
    <xsl:param name="extraGroup" />
    <xsl:element name="organizationalUnit">
      <xsl:call-template name="operationAttribute" />
      <xsl:apply-templates select="sourcedid" />
      <xsl:choose>
        <xsl:when test="$extraGroup">
          <orgUnitNumber>
            <xsl:value-of select="$extraGroup/extension/pifu_id[@type='organizationNumber']/pifu_value" />
          </orgUnitNumber>
          <orgUnitName>
            <xsl:value-of select="$extraGroup/extension/pifu_name[@type='orgName']/pifu_value" />
          </orgUnitName>
          <fullName>
            <xsl:value-of select="$extraGroup/description/short" />
          </fullName>
          <orgAcronym>
            <xsl:value-of select="$extraGroup/extension/pifu_name[@type='acronym']/pifu_value" />
          </orgAcronym>
          <email>
            <xsl:value-of select="$extraGroup/email" />
          </email>
          <xsl:apply-templates select="$extraGroup/extension/pifu_adr[@type='orgPostalAddress']/adr" />
          <phoneNbr>
            <xsl:value-of select="$extraGroup/extension/pifu_tel[@type='orgTelephone']" />
          </phoneNbr>
          <facsimileNbr>
            <xsl:value-of select="$extraGroup/extension/pifu_tel[@type='orgFax']" />
          </facsimileNbr>
        </xsl:when>
      </xsl:choose>
      <xsl:element name="relationship">
        <xsl:apply-templates select="relationship/sourcedid" />
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <xsl:template match="member">
    <xsl:element name="member">
      <xsl:apply-templates select="sourcedid" />
      <xsl:variable name="membergroupid" select="../sourcedid/id" />
      <xsl:variable name="membergroup" select="/enterprise/group[sourcedid/id=$membergroupid]" />
      <xsl:choose>
        <xsl:when test="$membergroup/grouptype/typevalue='alle ansatte'">
          <xsl:apply-templates select="role">
            <xsl:with-param name="feiderole" select="'staff'" />
          </xsl:apply-templates>
        </xsl:when>
        <xsl:when test="$membergroup/grouptype/typevalue='alle lærere'">
          <xsl:apply-templates select="role">
            <xsl:with-param name="feiderole" select="'staff'" />
          </xsl:apply-templates>
        </xsl:when>
        <xsl:when test="$membergroup/grouptype/typevalue='alle elever'">
          <xsl:apply-templates select="role">
            <xsl:with-param name="feiderole" select="'student'" />
          </xsl:apply-templates>
        </xsl:when>
      </xsl:choose>
    </xsl:element>
  </xsl:template>

  <xsl:template match="role">
    <xsl:param name="feiderole" />
    <xsl:element name="role">
      <xsl:call-template name="operationAttribute" />
      <xsl:value-of select="$feiderole" />
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>
